require(['common', 'rawCitiesData'], function () {
  // 证件选择
  $(".document-select").select({
    title: '选择证件类型',
    item: [{
      option: ['身份证', '护照', '港澳通行证', '回乡证', '军官证', '户口本', '出生证', '台胞证', '其他']
    }]
  });
  // 使用日期
  $(".use-date").on("click", function () {
    var self = $(this);
    $.pushHistory();
    $.calendar({
      self: self,
      isFull: false,
      curDate: self.data("date"),
      date: {
        '2018-11-11': { holiday: '节日', tag: 'qiang', price: parseInt(Math.random() * 10000) },
        '2018-11-28': { holiday: '节日', tag: 'qiang', price: parseInt(Math.random() * 10000) },
        '2018-11-29': { holiday: '节日', tag: 'cu', price: parseInt(Math.random() * 10000) },
        '2018-12-01': { holiday: '节日', tag: 'jian', price: parseInt(Math.random() * 10000) },
        '2018-12-02': { holiday: '节日', tag: 'jian', price: parseInt(Math.random() * 10000), num: 0 },
        '2018-12-03': { holiday: '节日', tag: 'jian', price: parseInt(Math.random() * 10000) },
        '2018-12-30': { holiday: '休', tag: 'cu', price: parseInt(Math.random() * 10000) },
        '2018-12-31': { holiday: '休', tag: 'cu', price: parseInt(Math.random() * 10000) },
        '2019-01-01': { holiday: '元旦', tag: 'qiang', price: parseInt(Math.random() * 10000) }
      },
      gridClick: function (date, self) {
        console.log(date);
        self.removeClass("placeholder");
      }
    })
  })
  // 场次选择
  $(".site-select").select({
    title: "选择场次",
    item: [{
      nav: '场次',
      option: ['08:00-10:00', '10:00-12:00', '12:00-14:00', '12:00-16:00', '16:00-18:00', '18:00-20:00', '20:00-22:00']
    }]
  });
  // 取票方式
  $(".collection-type").linkage({
    title: "取票方式",
    callback: function (val, target) {
      var nextAll = $(target).parents(".ticket-collection").nextAll();
      if (val === '快递到付') {
        nextAll.css("display", "flex");
      } else {
        nextAll.css("display", "none");
      }
    },
    linkage: [{
      name: '门店自取',
      sub: [{
        name: '天河区'
      }, {
        name: '白云区',
        sub: [{
          name: '乐嘉路店',
          con: '<div class="store-info"><h2>乐嘉路店</h2><ul>' +
            '<li><label>门店地址：</label><p>广东省广州市白云区机场西乐嘉路1号</p></li>' +
            '<li><label>营业时间：</label><p>周一至周日09:00~18:00</p></li>' +
            '<li><label>联系电话：</label><p>020-86338888</p></li>' +
            '</ul></div>'
        }, {
          name: '广之旅太阳新天地定制旅游俱乐部会员店',
          con: '<div class="store-info"><h2>乐嘉路店</h2><ul>' +
            '<li><label>门店地址：</label><p>广东省广州市白云区机场西乐嘉路1号</p></li>' +
            '<li><label>营业时间：</label><p>周一至周日09:00~18:00</p></li>' +
            '<li><label>联系电话：</label><p>020-86338888</p></li>' +
            '</ul></div>'
        }]
      }, {
        name: '越秀区'
      }, {
        name: '海珠区'
      }, {
        name: '荔湾区'
      }]
    }, {
      name: '快递到付'
    }]
  });
  // 所在地址
  $(".city-select").linkage({
    title: "选择地区",
    linkage: $.rawCitiesData
  });
  // 底部详情
  var priceData = {
    list: [
      { con: '成人票', price: 89999, num: 1 },
      { con: '儿童票', price: 8999, num: 1 },
      { con: '长者票', price: 899, num: 1 }
    ],
    serve: [
      { con: '外籍人士早餐附加费外籍人士早餐附加…', price: 8999, num: 200 },
      { con: '可选服务', price: 899, num: 20 },
      { con: '可选服务', price: 89, num: 2 }
    ],
    discounts: [
      { con: '早点优惠', price: 2000 },
      { con: '优惠信息', price: 200 }
    ]
  };
  var priceDetail = _.template($("#footbar-popup").html())(priceData);
  $(".gzl-footbar__bd").footPopup({
    title: '费用明细',
    titleAlign: 'center',
    content: priceDetail
  });
  // 门票数量
  $(".ticket-booking-num .gzl-count span").on("click", function () {
    if (!$(this).hasClass("disabled")) {
      var num, val, max, min;
      num = $(this).parent().find(".gzl-count__num");
      val = parseInt(num.text());
      max = $(this).data("max");
      min = $(this).data("min");
      if ($(this).hasClass("gzl-count__plus")) {// 加
        $(this).parent().find(".gzl-count__minus").removeClass("disabled");
        if (max && val < max) {
          num.text(val + 1);
          if (val + 1 === max) {
            $(this).addClass("disabled");
          }
        } else {
          num.text(val + 1);
        }
      } else if ($(this).hasClass("gzl-count__minus")) {// 减
        $(this).parent().find(".gzl-count__plus").removeClass("disabled");
        if (min && val > min) {
          num.text(val - 1);
          if (val - 1 === min) {
            $(this).addClass("disabled");
          }
        } else {
          num.text(val - 1);
        }
      }
    }
  })
  // 成人、儿童、长者
  $(".ticket-booking-numlist .gzl-count span").on("click", function () {
    if (!$(this).hasClass("disabled")) {
      var numlist, total, totalVal, max, min, num, val, type, selTpl, sellist;
      numlist = $(this).parents(".ticket-booking-numlist");
      total = numlist.find(".total-num");
      totalVal = parseInt(total.text());
      max = numlist.data("max");
      min = numlist.data("min");
      num = $(this).parent().find(".gzl-count__num");
      val = parseInt(num.text());
      type = $(this).parent().data("type");
      selTpl = _.template($("#tourist-select").html())({ type: type });
      sellist = $(".tourist-sel-list");
      if ($(this).hasClass("gzl-count__plus")) {// 加
        if (totalVal < max) {
          $(this).parent().find(".gzl-count__minus").removeClass("disabled");
          num.text(val + 1);
          total.text(totalVal + 1);
          if (totalVal + 1 === max) {
            numlist.find(".gzl-count__plus").addClass("disabled");
          }
          if (type === "adult") {
            if ($("[data-type='adult']", sellist).length < 1) {
              $(selTpl).prependTo(sellist);
            }
          } else if (type === "child") {
            if ($("[data-type='child']", sellist).length < 1) {
              if ($("[data-type='adult']", sellist).length < 1) {
                $(selTpl).prependTo(sellist);
              } else {
                $("[data-type='adult']", sellist).after(selTpl);
              }
            }
          } else {
            if ($("[data-type='elder']", sellist).length < 1) {
              sellist.append(selTpl);
            }
          }
        }
      } else if ($(this).hasClass("gzl-count__minus")) {// 减
        if (totalVal > min) {
          numlist.find(".gzl-count__plus").removeClass("disabled");
          num.text(val - 1);
          total.text(totalVal - 1);
          if (val - 1 === 0) {
            $(this).addClass("disabled");
            if (type === "adult") {
              $("[data-type='adult']", sellist).remove();
            } else if (type === "child") {
              $("[data-type='child']", sellist).last().remove();
            } else {
              $("[data-type='elder']", sellist).last().remove();
            }
          }
        }
      }
    }
  })
  // 联系人选择
  $(".tourist-sel-list").on("click", "li", function () {
    var contactData = {
      totality: 0,
      list: [{
        index: 0,checkType: null, checkState: 'uncheck',name: '克里斯蒂亚诺·罗纳', mobile: '13800138000',cardTypeName: '身份证',cardNm: '441246195505058984'
      }, {
        index: 1,checkType: null, checkState: 'uncheck',name: '克里斯蒂亚诺', mobile: '13800138001',cardTypeName: '身份证',cardNm: '440111198410201100'
      }, {
        index: 2,checkType: null, checkState: 'uncheck',name: '罗纳', mobile: '13800138002',cardTypeName: '港澳通行证',cardNm: '441246195505058984'
      }, {
        index: 3,checkType: null, checkState: 'uncheck',name: '测试',cardTypeName: '港澳通行证',cardNm: '441246195505058984'
      }]
    };
    var type = $(this).data("type");
    contactData.type = type;
    $(".tourist-info-list .booking-list-item").each(function () {
      var self = $(this);
      $(this).find("li").each(function () {
        contactData.list[$(this).data("index")].checkType = self.data("type");
        contactData.list[$(this).data("index")].checkState = 'check';
      })
    })
    var select = _.template($("#contact-select").html())(contactData);
    var contact = $(select).appendTo(document.body);
    var typeStr = {
      adult: '成人',
      child: '儿童',
      elder: '长者'
    }
    $.pushHistory();
    contact.show().addClass("show");
    // 选择
    $(".gzl-flex_acenter", contact).on("click", function () {
      var icon = $(this).find("use");
      var checkStatus = icon.attr("xlink:href");
      if(checkStatus !== "#icon-checked"){
        if (checkStatus === "#icon-uncheck") {
          icon.attr("xlink:href","#icon-check").parent().addClass("checked");
        }else if (checkStatus === "#icon-check"){
          icon.attr("xlink:href","#icon-uncheck").parent().removeClass("checked");
        }
      }
    })
    // 确认
    $(".confirm-btn", contact).on("click", function () {
      var tourist = {
        type: type,
        typeStr: typeStr[type],
        list: []
      };
      var touristList = $(".tourist-info-list");
      $(".contact-check .checked", contact).each(function (i) {
        if ($(this).find("use").attr("xlink:href") === "#icon-check"){
          var contactIndex = $(this).parents(".contact-item").data("index");
          tourist.list[i] = contactData.list[contactIndex];
        }
      })
      console.log(tourist)
      var tpl = _.template($("#tourist-list").html())(tourist);
      if($("li",touristList).length < 1){
        $(tpl).appendTo(touristList);
      }else if(type === 'adult'){
        $(".adult-info",touristList).remove();
        $(tpl).prependTo(touristList);
      }else if(type === 'child'){
        $(".child-info",touristList).remove();
        if($(".adult-info",touristList).length < 1){
          $(tpl).prependTo(touristList);
        } else {
          $(".adult-info",touristList).after(tpl);
        }
      }else if(type === 'elder'){
        $(".elder-info",touristList).remove();
        touristList.append(tpl);
      }
      contact.removeClass("show").transitionEnd(function () {
        this.remove();
      });
    })
    // 编辑
    $(".contact-edit,.contact-add", contact).on("click", function () {
      var editData = {};
      var editItem = _.template($("#contact-edit").html())(editData);
      var edit = $(editItem).appendTo(document.body);
      $.pushHistory();
      edit.show().addClass("show");
    })
    // 取消
    $(".cancel-btn", contact).on("click", function () {
      contact.removeClass("show").transitionEnd(function () {
        this.remove();
      });
    })
  })
  // 返回监听
  $.popstate(function () {
    var calendar = $(".gzl-calendar_popup").length < 1;
    var contactSel = $(".contact-select-page").length < 1;
    var contactEdit = $(".contact-edit-page").length < 1;
    if (calendar && contactSel && contactEdit) {
      window.history.go(-1);
    } else {
      if (!contactEdit) {
        $(".contact-edit-page").removeClass("show").transitionEnd(function () {
          this.remove();
        });
      } else {
        if(!calendar){
          $.calendarHide();
        }
        if(!contactSel){
          $(".contact-select-page").removeClass("show").transitionEnd(function () {
            this.remove();
          });
        }
      }
    }
  })
});